package com.zdsoft.blp.zongbuju.shouye.bianmin.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.bianmin.adapter.BMcxcxAdapter;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.BianMin;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class BMcxcxActivity extends BaseActivity implements NetCallback{
    private TextView tv_title;
    private GridView linli_gv;
    private List<BianMin> list;
    private BianMin bianMin;
    @Override
    protected void init() {
        setContentView(R.layout.linliquan);
        linli_gv = find(R.id.linli_gv);
        tv_title = find(R.id.tv_title);
        tv_title.setText("出行查询");
        Intent intent=getIntent();
        String code=intent.getStringExtra("code");
        Net net=new Net();
        net.url= API.BMINFO_URL;
        net.nameSpace=API.BMINFO_NAMESPACE;
        net.methodName=API.COLUMNCHILDLIST ;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String,Object> map=new HashMap<>();
        map.put("communityCode",communityCode);
        map.put("columnCode",code);
        net.parms=map;
        getServer(net,this);

        linli_gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(BMcxcxActivity.this,BMcxfsActivity.class);
               intent.putExtra("url",list.get(position).getUrl());
                intent.putExtra("title",list.get(position).getTitle());
                startActivity(intent);
            }
        });
    }
    @Override
    public void onResponse(boolean flag, int code, String res) {
        list=jiexi(res);
        linli_gv.setAdapter(new BMcxcxAdapter(BMcxcxActivity.this, list));
    }
    private List<BianMin> jiexi(String res) {
        list=new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            JSONArray array=object.getJSONArray("value");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String picPath=object2.getString("bmColumnImgPath");
                String url=object2.getString("url");
                String title=object2.getString("name");
                bianMin=new BianMin(title,picPath,url);
                list.add(bianMin);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }
}
